var http=require('http');
var url=require('url');
var util=require('util');
var queryString=require('querystring');
/*
http.createServer(function (request,response) {
    response.writeHead(200,{'Content-Type':'text/plain;charset=utf-8'});
    response.end(util.inspect(url.parse(request.url,true)));
}).listen(3000);

http.createServer(function (req,res) {
    res.writeHead(200,{'Content-Type':'text/plain;charset=utf-8'});
    var params=url.parse(req.url,true).query;
    res.write('网站名称:'+params.name);
    res.write('\n');
    res.write('网站URL：'+params.url);
    res.end();
}).listen(8080);*/

var postHTML =
    '<html><head><meta charset="utf-8"><title>菜鸟教程 Node.js 实例</title></head>' +
    '<body>' +
    '<form method="post">' +
    '网站名： <input name="name"><br>' +
    '网站 URL： <input name="url"><br>' +
    '<input type="submit">' +
    '</form>' +
    '</body></html>';

http.createServer(function (req,res) {
    var body='';
    req.on('data',function (chunk) {
        body+=chunk;
    });
    req.on('end',function () {
        body=queryString.parse(body);
        res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
        if(body.name&&body.url){
            res.write('网站名:'+body.name);
            res.write('<br/>');
            res.write('网站URL：'+body.url);
        }else {
            res.write(postHTML);
        }
        res.end();
    });
}).listen(8080);